package com.qd.marketlist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MyAdapter extends SimpleCursorAdapter {
    private static final boolean DBG = MLConst.DEBUG;
    private Context mContext;
    private int mLayoutId;
    private Cursor mCursor;
    private Uri mUri;
    private View.OnClickListener mListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ContentValues values = new ContentValues();
            CheckBox cb = (CheckBox)v;
            Long id = (Long)cb.getTag();

            values.put(MLConst.DBSTR_COL_CHECKBOX_STATE,
                    cb.isChecked()?MLConst.DB_STATE_CHECKED:MLConst.DB_STATE_UNCHECKED);
            
            mContext.getContentResolver().update(
                    mUri,
                    values,
                    MLConst.DBSTR_COL_ID + "=?",
                    new String[]{id.toString()});
        }
        
    };

    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
        // TODO Auto-generated constructor stub
        mContext = context;
        mLayoutId = layout;
        mCursor = c;
    }

    public void setUri(Uri uri){
        mUri = uri;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mLayoutId, null);
        CheckBox chb = (CheckBox)convertView.findViewById(R.id.lv_Checkbox);
        TextView tv = (TextView)convertView.findViewById(R.id.lv_Title);

        mCursor.moveToPosition(position);
        tv.setText(mCursor.getString(mCursor.getColumnIndex(MLConst.DBSTR_COL_NAME)));
        int isChecked = mCursor.getInt(mCursor.getColumnIndex(MLConst.DBSTR_COL_CHECKBOX_STATE));
        chb.setTag(mCursor.getLong(0));//checkbox index bug fixed
        
        chb.setOnClickListener(mListener);
        /*chb.setOnCheckedChangeListener(
                new OnCheckedChangeListener(){

                    @Override
                    public void onCheckedChanged(CompoundButton arg0,
                            boolean arg1) {
                        // TODO Auto-generated method stub
                       // DBHelper dbHelper = new DBHelper(mContext);
                        ContentValues values = new ContentValues();
                        Long id = (Long)arg0.getTag();

                        values.put(MLConst.DBSTR_COL_CHECKBOX_STATE,
                                arg1?MLConst.DB_STATE_CHECKED:MLConst.DB_STATE_UNCHECKED);

                        //dbHelper.getWritableDatabase()
                        //    .update(MLConst.DB_TABLE_NAME, values, "_id=?", new String[]{id.toString()});
                        //dbHelper.getWritableDatabase().close();
                        mContext.getContentResolver().update(
                                MyProvider.CONTENT_URI,
                                values,
                                MLConst.DBSTR_COL_ID + "=?",
                                new String[]{id.toString()});
                    }
                    
                });*/
        chb.setChecked(MLConst.DB_STATE_CHECKED == isChecked?true:false);
        return convertView;
    }
}
